package PureAnnotations.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
@Aspect
public class MyAspect {
    @Pointcut("execution(public * PureAnnotations.service..addUser(..))")
    public void addUserPointCut(){}

    @Pointcut("execution(* PureAnnotations.service..deleteUser*(..))")
    public void deletePointCut(){}

    @Pointcut("execution(* PureAnnotations.service..updateUser*(..))")
    public void updatePointCut(){}

    @Before("addUserPointCut() || deletePointCut() || updatePointCut()")
    public void beforeOperate(JoinPoint joinPoint){
        System.out.println(getTime()+" XX 正在执行 "+joinPoint.getSignature().getName()+"方法");
    }

    public String getTime(){
        // 获取当前的日期和时间
        LocalDateTime currentTime = LocalDateTime.now();
        // 定义日期时间格式（可选）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化当前时间为指定格式的字符串（可选）
        return currentTime.format(formatter);
    }
}
